﻿// WebZHX.Core/Interfaces/IRepository.cs
using Common.Models;
using Core.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Core.Interfaces
{
    /// <summary>
    /// 基础仓储接口
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    /// <typeparam name="TKey">主键类型</typeparam>
    public interface IBaseRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>
    {
        /// <summary>
        /// 根据主键获取实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        Task<TEntity> GetByIdAsync(TKey id);

        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns>实体列表</returns>
        Task<IEnumerable<TEntity>> GetAllAsync();

        /// <summary>
        /// 新增实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>新增的实体</returns>
        Task<TEntity> AddAsync(TEntity entity);

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>更新后的实体</returns>
        Task<TEntity> UpdateAsync(TEntity entity);

        /// <summary>
        /// 局部更新实体（只更新非null字段）
        /// </summary>
        Task<TEntity> PartialUpdateAsync(TEntity entity);


        /// <summary>
        /// 删除实体（物理删除）
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>是否成功</returns>
        Task<bool> DeleteAsync(TKey id);

        /// <summary>
        /// 软删除实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>是否成功</returns>
        Task<bool> SoftDeleteAsync(TKey id);
    }

    /// <summary>
    /// 分页仓储接口
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    /// <typeparam name="TKey">主键类型</typeparam>
    public interface IPagedRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>
    {
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="orderBy">排序字段</param>
        /// <returns>分页结果</returns>
        Task<PagedResult<TEntity>> GetPagedAsync(int pageIndex, int pageSize, string orderBy = "Id");

        /// <summary>
        /// 分页查询（带过滤条件）
        /// </summary>
        /// <param name="filter">查询过滤器</param>
        /// <returns>分页结果</returns>
        Task<PagedResult<TEntity>> GetPagedAsync(QueryFilter filter);
    }

    /// <summary>
    /// Dapper仓储接口
    /// </summary>
    public interface IDapperRepository
    {
        /// <summary>
        /// 执行查询，返回多个实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>实体列表</returns>
        Task<IEnumerable<T>> QueryAsync<T>(string sql, object parameters = null);

        /// <summary>
        /// 执行查询，返回单个实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>实体</returns>
        Task<T> QueryFirstOrDefaultAsync<T>(string sql, object parameters = null);

        /// <summary>
        /// 执行命令（增、删、改）
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>影响的行数</returns>
        Task<int> ExecuteAsync(string sql, object parameters = null);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="orderBy">排序字段</param>
        /// <returns>分页结果</returns>
        Task<PagedResult<T>> QueryPagedAsync<T>(string sql, object parameters, int pageIndex, int pageSize, string orderBy = "Id");
    }
}